package com.xrtn.service.impl;

import com.xrtn.bean.FunctionInfo;
import com.xrtn.bean.SysUserInfo;
import com.xrtn.bean.TBookingInfo;
import com.xrtn.mapper.SysRoleInfoMapper;
import com.xrtn.mapper.SysRoleMenuRelMapper;
import com.xrtn.mapper.SysUserInfoMapper;
import com.xrtn.service.ISysAuthService;
import com.xrtn.utils.CodeEnum;
import com.xrtn.utils.MD5Utils;
import com.xrtn.utils.ObjUtils;
import com.xrtn.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysAuthServiceImpl implements ISysAuthService {

    /**
     * 系统用户表
     */
    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Autowired
    private SysRoleInfoMapper roleInfoMapper;

    /**
     * 登录
     *
     * @param account
     * @param userPwd
     * @return
     */

    @Override
    public Map<String, Object> sysSign(String account, String userPwd) {

        Map<String, Object> params = new HashMap<>();
        params.put("userNo", account);
        params.put("userStatus", 0);
        SysUserInfo sysUserInfo = sysUserInfoMapper.selectByUserInfo(params);

        if (ObjUtils.isNotEmpty(sysUserInfo)) {
            //如果密码不为空
            if (ObjUtils.isNotEmpty(sysUserInfo.getUserPwd())) {
                // 如果密码匹配成功
                if (userPwd.equals(sysUserInfo.getUserPwd())) {

                    List<Map<String,Object>> userFunctionList= roleInfoMapper.selectRoleLists(sysUserInfo.getUserRole());
                    List<String> groupList = new ArrayList<>();

                    boolean isLogin = false;

                    for (Map<String,Object> functionMap : userFunctionList){
                        groupList.add(functionMap.get("menuName").toString());
                        if(Integer.parseInt(functionMap.get("functionId").toString()) == 46){
                            isLogin = true;
                        }
                    }
                    if(isLogin == false){
                        params.put("EnumCode", CodeEnum.CODE_10011);
                        return params;
                    }

                    List<String> tempList = new ArrayList(new HashSet(groupList));
                    List<FunctionInfo> retMap = new ArrayList<>();
                    for (String menuName : tempList){
                        FunctionInfo info = new FunctionInfo();
                        Map<String, Object> typeMap = new HashMap<>();
                        Integer menuId = 0;
                        List<Map<String,Object>> list = new ArrayList<>();
                        for (Map<String,Object> functionMap : userFunctionList){
                            menuId = Integer.parseInt(functionMap.get("menuId").toString());
                            if(functionMap.get("menuName").toString().equals(menuName)){
                                list.add(functionMap);
                            }
                        }
                        info.setMenuName(menuName);
                        info.setMenuId(menuId);
                        info.setFunctionList(list);
                        typeMap.put("menuName",menuName);
                        typeMap.put("menuId",menuId);
                        typeMap.put("functionList",list);
                        retMap.add(info);
                    }

                    sysUserInfo.setRoleFunction(retMap);
                    //用户对象
                    params.put("userInfo", sysUserInfo);
                    return params;
                } else {
                    // 否则说明密码错误
                    params.put("EnumCode", CodeEnum.CODE_10002);
                    return params;
                }
            } else {
                //否则需要进行注册
                params.put("EnumCode", CodeEnum.CODE_10007);
                return params;
            }
        } else {
            // 那么用户不存在
            params.put("EnumCode", CodeEnum.CODE_10003);
            return params;
        }
    }

    @Override
    public int updatePwd(int userId, String oldPwd, String userPwd) {

        if (6 <= userPwd.length() && userPwd.length() < 20) {
            Map<String, Object> params = new HashMap<>();
            params.put("userId", userId);
            params.put("userStatus", 0);
            SysUserInfo sysUserInfo = sysUserInfoMapper.selectByUserInfo(params);
            if (ObjUtils.isNotEmpty(sysUserInfo)) {
                //如果密码不为空
                if (ObjUtils.isNotEmpty(sysUserInfo.getUserPwd())) {
                    // 如果密码匹配成功
                    if (MD5Utils.md5Password(oldPwd).equals(sysUserInfo.getUserPwd())) {
                        // 修改密码
                        params.put("userPwd", MD5Utils.md5Password(userPwd));
                        int count = sysUserInfoMapper.updateStatusByKey(params);
                        return count;
                    } else {
                        // 否则说明密码错误
                        return -2;
                    }
                }
                return -1;
            }
            return -1;
        } else {
            return -1;
        }
    }

    /**
     * 重置密码
     *
     * @param userId
     * @return
     */
    @Override
    public ResponseData updateUserPwd(Integer userId, ResponseData data) {

        SysUserInfo userInfo = sysUserInfoMapper.selectByPrimaryKey(userId);

        if(ObjUtils.isEmpty(userInfo)){
            data.setCodeEnum(CodeEnum.CODE_10003);
            return data;
        }

        userInfo.setUserPwd(MD5Utils.md5Password("123456"));
        sysUserInfoMapper.updateByPrimaryKeySelective(userInfo);
        return data;
    }
}
